package com.small.context;

import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import com.small.exception.IException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/** Context全局上下文对象
 * @author 乐乐
 */
public class Context {
    private static final Logger logger = Logger.getLogger(Context.class);

    private static final Object object = new Object();
    
    private HttpServletRequest httpServletRequest;
    private HttpServletResponse httpServletResponse;
    private JSONObject parameter = null;    //请求参数
    private JSONObject response = null;     //响应参数
    private String requestType = null;      //请求参数类型
    private List<InputStream> inputStreams = new ArrayList<InputStream>(); //输入流
    private byte[] binary = null; //二进制流 用于响应
    private String sendRedirect = ""; //，重定向用于跳转页面

    public Context(){}
    
    public void fail(String err) {
      throw new IException(err);
    }

    public HttpServletRequest getHttpServletRequest() {
        return this.httpServletRequest;
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public HttpServletResponse getHttpServletResponse() {
        return this.httpServletResponse;
    }

    public void setHttpServletResponse(HttpServletResponse httpServletResponse) {
        this.httpServletResponse = httpServletResponse;
    }

    public JSONObject getParameter() {
        return parameter;
    }

    public void setParameter(JSONObject parameter) {
        this.parameter = parameter;
    }

    public JSONObject getResponse() {
        return this.response;
    }

    /**向页面响应参数
     *
     * @param response
     * @param <T>  泛型，支持json、数组、字符串等数据
     */
    public <T> void setResponse(T response) {
      try {
        logger.debug("开始响应数据...");
        PrintWriter print = this.httpServletResponse.getWriter();
        print.print(response);
        print.flush();
        print.close();
      }catch (IOException io){
        logger.error(io.toString());
      }
      logger.debug("响应成功...");
    }

    public String getRequestType() {
        return this.requestType;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public List<InputStream> getInputStreams() {
        return this.inputStreams;
    }

    public void setInputStreams(List<InputStream> inputStreams) {
        this.inputStreams = inputStreams;
    }

    public byte[] getBinary() {
        return this.binary;
    }

    /**
     * 响应二进制数据
     * @param binary
     */
    public void setBinary(byte[] binary) {
      synchronized (object) {
        logger.debug("开始响应二进制数据...");
        //清除之前的流信息， 防止影响以下响应操作
        try {
            this.httpServletResponse.reset();
            this.httpServletResponse.getOutputStream().write(binary);
            this.httpServletResponse.getOutputStream().flush();
            this.httpServletResponse.getOutputStream().close();
        }catch (IOException io){
            logger.error(io.toString());
        }
        logger.debug("响应完成...");
      }
    }
    
    public String getSendRedirect() {
      return this.sendRedirect;
    }
    
    public void setSendRedirect(String sendRedirect) {
      this.sendRedirect = sendRedirect;
    }
    
}
